package com.rytong.tools.animation;

import java.util.HashMap;

import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class EmpAnimation extends Animation {
    // 动画的初始对应状态表
    public HashMap fromHash_;
    // 动画的结束对应状态表
    public HashMap toHash_;

    boolean isSkew_;

    float skewXFromAngle_;
    float skewXToAngle_;
    float skewYFromAngle_;
    float skewYToAngle_;
    
    boolean isXDisappear_ = false;//标示 最后斜拉的角度大于90度
    boolean isYDisappear_ = false;

    public float[] fromMatrix_;
    public float[] toMatrix_;
    
    //动画中心点
    float px_;
    float py_;
            
    public void setSkew(boolean iswork, float xfromangle, float xtoangle , 
    		float yfromangle , float ytoangle ,float px , float py) {
        isSkew_ = iswork;
        skewXFromAngle_ = xfromangle;
        if(xfromangle > 90f){
        	skewXFromAngle_ = 90f;
        } else if(xfromangle < -90f){
        	skewXFromAngle_ = -90f;
        }
        skewXToAngle_ = xtoangle;
        if(xtoangle >= 90f){
        	skewXToAngle_ = 89f;
        	isXDisappear_ = true;
        } else if(xtoangle <= -90f){
        	skewXToAngle_ = -89f;
        	isXDisappear_ = true;
        }
        skewYFromAngle_ = yfromangle;
        if(yfromangle > 90f){
        	skewYFromAngle_ = 90f;
        } else if(yfromangle < -90f){
        	skewYFromAngle_ = -90f;
        }
        skewYToAngle_ = ytoangle;
        if(ytoangle >= 90f){
        	skewYToAngle_ = 89f;
        	isYDisappear_ = true;
        } else if(ytoangle <= -90f){
        	skewYToAngle_ = -89f;
        	isYDisappear_ = true;
        }
        px_ = px;
        py_ = py;
    }
    
    public void setMatrix(float[] from , float[] to , float px , float py){
        fromMatrix_ = from;
        if(fromMatrix_ == null){
        	fromMatrix_ = new float[]{1f,0f,0f,0f,1f,0f,0f,0f,1f};
        }
        toMatrix_ = to;
        
        px_ = px;
        py_ = py;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        Matrix matrix = t.getMatrix();
        if (isSkew_) {
        	float fromX = (float)Math.tan(skewXFromAngle_ * Math.PI / 180);
        	float toX = (float)Math.tan(skewXToAngle_ * Math.PI / 180);
        	float fromY = (float)Math.tan(skewYFromAngle_ * Math.PI / 180);
        	float toY = (float)Math.tan(skewYToAngle_ * Math.PI / 180);
        	matrix.postSkew(fromX + (toX - fromX) * interpolatedTime
        	        , fromY + (toY - fromY) * interpolatedTime
        	        , px_,py_);
        	//直接斜拉到90度动画效果有问题，暂先斜拉到89度，最后再做90度的倾斜
        	if(interpolatedTime == 1){
        	    if(isXDisappear_ && isYDisappear_){
        	        matrix.postSkew((float)Math.tan(90 * Math.PI / 180)
        	                , (float)Math.tan(90 * Math.PI / 180)
        	                , px_,py_);
        	    } else if(isXDisappear_){
        	        matrix.postSkew((float)Math.tan(90 * Math.PI / 180)
        	                , (float)Math.tan(skewYToAngle_ * Math.PI / 180)
        	                , px_,py_);
        	    } else if(isYDisappear_){
        	        matrix.postSkew((float)Math.tan(skewXToAngle_ * Math.PI / 180)
        	                , (float)Math.tan(90 * Math.PI / 180)
        	                , px_,py_);
        	    }
        	}
        }
        //矩阵动画
        if(toMatrix_ != null){
           float[] to = new float[] {1f , 0f , 0f , 0f , 1f , 0f , 0f , 0f , 1f};
           for(int i = 0 ; i < toMatrix_.length ; i++){
        	   to[i] = fromMatrix_[i] + (toMatrix_[i] - fromMatrix_[i]) * interpolatedTime;
           }
           matrix.setValues(to);
           matrix.preTranslate(-px_, -py_);   
           matrix.postTranslate(px_, py_); 
        }
    }
}
